package com.antrain.his.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.antrain.his.annotation.UserLoginToken;
import com.antrain.his.entity.Herbaltempdetailed;
import com.antrain.his.service.IHerbaltempdetailedService;
import com.antrain.his.utils.InitUtil;
import com.antrain.his.utils.Result;
import com.antrain.his.utils.ResultGenerator;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.web.bind.annotation.*;
/**
 * <p>
 * 草药模板明细  前端控制器
 * </p>
 *
 * @author antrain
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/herbaltempdetailed")
public class HerbaltempdetailedController {
  @Resource
  private IHerbaltempdetailedService herbaltempdetailedService;

  /**
   * 进行分页查询 
   * HerbaltempdetailedName 可选 药品名称
   */
  @GetMapping
  public Result getlist(@RequestParam Map<String, Object> param) {
    InitUtil.initPage(param);
    int num = Integer.parseInt(param.get("page").toString());
    int limit = Integer.parseInt(param.get("limit").toString());
    QueryWrapper<Herbaltempdetailed> wrapper = new QueryWrapper<>();
    IPage<Herbaltempdetailed> page = new Page<>(num, limit);
    return ResultGenerator.getSuccessResult(herbaltempdetailedService.page(page, wrapper));
  }

  @GetMapping("/{id}")
  public Result getHerbaltempdetailed(@PathVariable int id) {
    Herbaltempdetailed herbaltempdetailed = herbaltempdetailedService.getById(id);
    if (herbaltempdetailed == null)
      return ResultGenerator.getFailResult("", "无该草药模板明细记录");
    return ResultGenerator.getSuccessResult(herbaltempdetailed);
  }

  /**
   * name : 需要检查那个字段的value是否在数据库存在
   * 比如/check/HerbaltempdetailedName?value=阿司匹林  ：检查数据库是否存在HerbaltempdetailedName='阿司匹林' 
   */
  @GetMapping("/check/{name}")
  public Result checkName(@PathVariable String name,@RequestParam String value){
    QueryWrapper<Herbaltempdetailed> wrapper = new QueryWrapper<>();
    wrapper.eq(name, value);
    if (herbaltempdetailedService.getOne(wrapper) != null)
      return ResultGenerator.getFailResult("", "该记录已存在，请重新选择");
    return ResultGenerator.getSuccessResult();
  }

  @PostMapping()
  @UserLoginToken
  public Result save(@RequestBody Herbaltempdetailed herbaltempdetailed) {
    if (herbaltempdetailedService.save(herbaltempdetailed))
      return ResultGenerator.getSuccessResult("", "添加成功");
    return ResultGenerator.getFailResult("", "添加失败");
  }

  @PutMapping("/{id}")
  @UserLoginToken
  public Result update(@RequestBody Herbaltempdetailed herbaltempdetailed, @PathVariable int id) {
    herbaltempdetailed.setId(id);
    if (herbaltempdetailedService.updateById(herbaltempdetailed))
      return ResultGenerator.getSuccessResult("", "更新成功");
    return ResultGenerator.getFailResult("", "更新失败");
  }

  @DeleteMapping("/{id}")
  @UserLoginToken
  public Result del(@PathVariable int id) {
    if (herbaltempdetailedService.removeById(id))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }

  @DeleteMapping("/batchdel")
  @UserLoginToken
  public Result batchDel(@RequestParam String ids) {
    String[] idList = ids.split(",");
    List<Integer> list = new ArrayList<>(idList.length);
    for (String id : idList) {
      list.add(Integer.parseInt(id));
    }
    if (herbaltempdetailedService.removeByIds(list))
      return ResultGenerator.getSuccessResult("", "删除成功");
    return ResultGenerator.getFailResult("", "删除失败");
  }
}
